var teamId;
var tmGameId;
var gameDataList={};
var tmMemberList={};
var dataItemList={};
$(document).ready(function(){
	$.ajaxSettings.async = false;
	$('#header').animate({'top':'0px'},400);
	teamId = args.teamId; //取得id的值
	tmGameId = args.tmGameId;
	var paraData = {teamId:teamId,tmGameId:tmGameId};
	//查找可选的人员
	$.getJSON(path+"/soccer/tmMemberAction!selectByTeam.action",paraData, function(data){
		  tmMemberList = data.tmMemberList;
	});
	//查找数据项
	$.getJSON(path+"/soccer/tmGameDataAction!selectAllDataItem.action",paraData, function(data){
		  dataItemList = data.dataItemList;
	});
	
	refreshGameData();
	
	$("#addRowBtn").click(function(){
		//数组对象增加一个控制
		var gameData = {"createdBy":-1,"createdDate":null,"deletedFlag":"0","itemId":null,"itemValue":1,"lastUpdBy":-1,"lastUpdDate":null,"modificationNum":0,"originApp":"","originFlag":"","playUserId":-1,"recordDate":null,"recordUserId":-1,"remark":"","rowId":-1,"teamId":teamId,"tmGameId":tmGameId};
		gameDataList.push(gameData);
		addRow(gameDataList.length-1,gameData);
	});
	$("#saveBtn").click(function(){
		saveGameData();
	});
});
function refreshGameData(){
	var paraData = {teamId:teamId,tmGameId:tmGameId};
	$.getJSON(path+"/soccer/tmGameDataAction!selectGameDataByGame.action",paraData, function(data){
		$("#gameDataBody").html("");
		gameDataList = data.gameDataList;
	  	$.each(gameDataList,function(index,obj){
	  		if(obj.deletedFlag!='1'){
	  			addRow(index,obj);
	  		}
	  	});  
	});
}
function goBack(){
	window.location.href=path+"/soccer/teamGame.jsp?teamId="+teamId+"&tmGameId="+tmGameId;
}
function add(index){
	var t = $("input[id='gameDataList["+index+"].itemValue']");
	var itemValue = parseFloat(t.val());
	t.val(isNaN(itemValue)?1:itemValue+1);
	itemValueChanged(index);
}
function minus(index){
	var t = $("input[id='gameDataList["+index+"].itemValue']");
	var itemValue = parseFloat(t.val());
	t.val(isNaN(itemValue)?0:itemValue-1);
	itemValueChanged(index);
}
function itemValueChanged(index){
	var t = $("input[id='gameDataList["+index+"].itemValue']");
	var itemValue = parseFloat(t.val());
	if(itemValue<0){
		alert("数值不能小于0。");
		t.val(gameDataList[index].itemValue);
	}else{
		gameDataList[index].itemValue=itemValue;
	}
}
function playUserChanged(index,obj){
	gameDataList[index].playUserId=obj.value;
	$.each(tmMemberList,function(index1,tmpTmMember){
		if(tmpTmMember.logUserId==obj.value){debugger;
			$("#gameDataList["+index+"].playUserNo").html(tmpTmMember.playerNo);
		}
	});
}
function dataItemChanged(index,obj){
	gameDataList[index].itemId=obj.value;
}
function saveGameData(){
	var jsonStr = JSON.stringify(gameDataList);
	$.post(path+"/soccer/tmGameDataAction!gameDataSave.action",{teamId:teamId,tmGameId:tmGameId,jsonStr:jsonStr}, function(data){
		strMessage = data.strMessage;
		$('#strMessage').text(strMessage);
		
		refreshGameData();
		
		alert(strMessage);
		//window.location.reload();
	},'json');
}
function deleteGameData(index){
	//var trObj = $("#gameDataList["+index+"].tr");
	//trObj.parent().remove();
	gameDataList[index].deletedFlag='1';
	saveGameData();
}
function addRow(index,gameData){//debugger;
	var gameDataHtml=[];
	gameDataHtml.push("<tr id='gameDataList["+index+"].tr'>");
	gameDataHtml.push("<td id='gameDataList["+index+"].playUserNo' nowrap='nowrap' style='font-size:10px'>");
	$.each(tmMemberList,function(index1,tmpTmMember){
		if(gameData.playUserId==tmpTmMember.logUserId){
			gameDataHtml.push(tmpTmMember.playerNo);
		}
	});
	gameDataHtml.push("</td>");
	gameDataHtml.push("<td style='font-size:10px'>");
	gameDataHtml.push("<select id='gameDataList["+index+"].playUserId' onchange='playUserChanged("+index+",this)'>");
	gameDataHtml.push("<option value='-1'>请选择</option>");
	$.each(tmMemberList,function(index1,tmpTmMember){
		if(gameData.playUserId==tmpTmMember.logUserId){
			gameDataHtml.push("<option value='"+tmpTmMember.logUserId+"' selected='true'>"+tmpTmMember.logUserName+"</option>");
		}else{
			gameDataHtml.push("<option value='"+tmpTmMember.logUserId+"'>"+tmpTmMember.logUserName+"</option>");
		}
	});
	gameDataHtml.push("</select>");
	gameDataHtml.push("</td>");
	//数据项
	gameDataHtml.push("<td style='font-size:10px'>");
	gameDataHtml.push("<select id='gameDataList["+index+"].itemId' onchange='dataItemChanged("+index+",this)'>");
	gameDataHtml.push("<option value='-1'>请选择</option>");
	$.each(dataItemList,function(index1,tmpDataItem){
		if(gameData.itemId==tmpDataItem.rowId){
			gameDataHtml.push("<option value='"+tmpDataItem.rowId+"' selected='true'>"+tmpDataItem.itemName+"</option>");
		}else{
			gameDataHtml.push("<option value='"+tmpDataItem.rowId+"'>"+tmpDataItem.itemName+"</option>");
		}
	});
	gameDataHtml.push("</select>");
	gameDataHtml.push("</td>");
//	gameDataHtml.push("</tr>");
//	gameDataHtml.push("<tr id='gameDataList["+index+"].tr_2'>");
	//分值
	gameDataHtml.push("<td nowrap='nowrap' style='font-size:10px;height:10px'>");
	gameDataHtml.push("<input id='min' name='' type='button'  value='-' onclick='minus("+index+")'/>");
	gameDataHtml.push("<input id='gameDataList["+index+"].itemValue' onchange='itemValueChanged("+index+")' onkeydown='validateNumber(event,"+index+")' value='"+gameData.itemValue+"' type='number' onclick='this.select();' />");
	gameDataHtml.push("<input id='add' name='' type='button' value='+' onclick='add("+index+")'/>");
	gameDataHtml.push("</td>");
	gameDataHtml.push("<td nowrap='nowrap' onclick='deleteGameData("+index+")' style='font-size:10px'>删除</td>");
	gameDataHtml.push("</tr>");
	
	$("#gameDataBody").html($("#gameDataBody").html()+gameDataHtml.join(""));
}
//限制只能输入数字
function validateNumber(e,index){
	var code = parseInt(window.event ? e.keyCode:e.which);
    if (code >= 96 && code <= 105 || code >= 48 && code <= 57 || code == 8 || code==110 || code==190) {  
        return true;  
    } else {  
    	alert("只能输入小数！");
    	var t = $("input[id='gameDataList["+index+"].itemValue']");
    	t.val(gameDataList[index].itemValue);
        return false;
    }
}